package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.manager.SceneOperateManager;
import com.qiantu.phone.widget.SimpleAnimationDraweeView;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/09/22
 * desc   : 房间标签数据列表
 */
public final class HomeSceneAdapter extends AppAdapter<SceneBean> {

    private int totalWidth;
    private boolean needShowFloorRoom;
    private Handler mHandler;

    public HomeSceneAdapter(Context context, int totalWidth, boolean needShowFloorRoom) {
        super(context);
        this.totalWidth = totalWidth;
        this.needShowFloorRoom = needShowFloorRoom;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final SimpleAnimationDraweeView iconScene;
        private final TextView tvSceneName;
        private final TextView tvFloorRoom;

        private ViewHolder() {
            super(R.layout.home_scene_item);
            iconScene = findViewById(R.id.icon_scene);
            tvSceneName = findViewById(R.id.tv_scene_name);
            tvFloorRoom = findViewById(R.id.tv_floor_room);
            itemView.setOnClickListener(mOnClickListener);
            RecyclerView.LayoutParams rl = (RecyclerView.LayoutParams) itemView.getLayoutParams();
            rl.width = (int) (totalWidth / 4f);
        }

        @Override
        public void onBindView(int position) {
            SceneBean scene = getItem(position);
            tvSceneName.setText(scene.getName());
            if (needShowFloorRoom) {
                tvFloorRoom.setVisibility(View.VISIBLE);
                tvFloorRoom.setText(LLRoomDBManager.getInstance(getContext()).getFloorRoomName(scene.getRoomSerialNo()));
            } else {
                tvFloorRoom.setVisibility(View.GONE);
            }
//            GlideApp.with(getContext())
//                    .load(scene.getImage())
//                    .into(iconScene);
            String staticImage = scene.getImage();
            iconScene.setImageURI(staticImage);
            itemView.setTag(position);
            itemView.setEnabled(true);
        }
    }

    private SceneOperateManager mSceneOperateManager;
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            SceneBean scene = getData().get(position);
            if(mSceneOperateManager == null){
                mSceneOperateManager = new SceneOperateManager(getContext());
            }
            mSceneOperateManager.onClick(v, scene.getSceneSerialNo());
        }
    };
}